package org.examat.web.beans;

import java.io.Serializable;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
import javax.naming.InitialContext;
import javax.persistence.PersistenceContext;
import javax.persistence.EntityManager;
import javax.transaction.UserTransaction;
import javax.validation.constraints.AssertTrue;
import javax.validation.constraints.Pattern;
import org.examat.entidades.Alumno;
import org.examat.entidades.Profesor;

/**
 *
 * @author Fer
 */
@Named
@SessionScoped
public class RegistroController
    implements Serializable
{
    private String nombre;
    private String aPaterno;
    private String aMaterno;
    private Integer claveUnica;
    private String password1;
    private String password2;    
    private String tipo;
    @Pattern(regexp="[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?", message="Email inválido")
    private String email;
    @PersistenceContext
    private EntityManager em;
    private String message;
    private boolean passwordsIguales;

    
    @AssertTrue(message="Los Passwords deben ser iguales")
    public boolean getPasswordsIguales()
    {
        System.out.println("prueba GET de paswords p1=" + this.password1 + " p2=" +this.password2);
        this.passwordsIguales = password1!=null && password1.equals(password2);
        return this.passwordsIguales;
    }
    
    public void setPasswordsIguales(boolean v)
    {
        System.out.println("prueba SET de paswords p1=" + this.password1 + " p2=" +this.password2);
        this.passwordsIguales = password1!=null && password1.equals(password2);
    }
    /**
     * Get the value of message
     *
     * @return the value of message
     */
    public String getMessage() {
        return message;
    }

    /**
     * Set the value of message
     *
     * @param message new value of message
     */
    public void setMessage(String message) {
        this.message = message;
    }


    /**
     * Get the value of password2
     *
     * @return the value of password2
     */
    public String getPassword2() {
        return password2;
    }

    /**
     * Set the value of password2
     *
     * @param password2 new value of password2
     */
    public void setPassword2(String password2) {
        this.password2 = password2;
    }

    /**
     * Get the value of email
     *
     * @return the value of email
     */
    public String getEmail() {
        return email;
    }

    /**
     * Set the value of email
     *
     * @param email new value of email
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * Get the value of tipo
     *
     * @return the value of tipo
     */
    public String getTipo() {
        return tipo;
    }

    /**
     * Set the value of tipo
     *
     * @param tipo new value of tipo
     */
    public void setTipo(String tipo) {
        this.tipo = tipo;
    }

    /**
     * Get the value of password
     *
     * @return the value of password
     */
    public String getPassword1() {
        return password1;
    }

    /**
     * Set the value of password
     *
     * @param password new value of password
     */
    public void setPassword1(String password1) {
        this.password1 = password1;
    }

    /**
     * Get the value of claveUnica
     *
     * @return the value of claveUnica
     */
    public Integer getClaveUnica() {
        return claveUnica;
    }

    /**
     * Set the value of claveUnica
     *
     * @param claveUnica new value of claveUnica
     */
    public void setClaveUnica(Integer claveUnica) {
        this.claveUnica = claveUnica;
    }

    /**
     * Get the value of aMaterno
     *
     * @return the value of aMaterno
     */
    public String getAMaterno() {
        return aMaterno;
    }

    /**
     * Set the value of aMaterno
     *
     * @param aMaterno new value of aMaterno
     */
    public void setAMaterno(String aMaterno) {
        this.aMaterno = aMaterno;
    }

    /**
     * Get the value of aPaterno
     *
     * @return the value of aPaterno
     */
    public String getAPaterno() {
        return aPaterno;
    }

    /**
     * Set the value of aPaterno
     *
     * @param aPaterno new value of aPaterno
     */
    public void setAPaterno(String aPaterno) {
        this.aPaterno = aPaterno;
    }

    /**
     * Get the value of Nombre
     *
     * @return the value of Nombre
     */
    public String getNombre() {
        return nombre;
    }

    /**
     * Set the value of Nombre
     *
     * @param Nombre new value of Nombre
     */
    public void setNombre(String Nombre) {
        this.nombre = Nombre;
    }

    public String registra() 
    {
        System.out.println(tipo);
        if(tipo.equals("alumno")) {
            Alumno al = new Alumno();
            al.setNombre(nombre);
            al.setAPaterno(aPaterno);
            al.setAMaterno(aMaterno);
            al.setClaveUnica(claveUnica);
            al.setEmail(email);
            al.setPassword(password1);
            System.out.println("puse todas");
            try {
                UserTransaction tx = (UserTransaction) new InitialContext().lookup("UserTransaction");
                tx.begin();
                    em.joinTransaction();
                    em.persist(al);
                tx.commit();
                message = "Registro de alumno satisfactorio";
                System.out.println("guardadao");
                return null; //cambiar
            }
            catch(Exception e) {
                System.out.println("otro");
                System.out.println("FER: "+e.toString());
                message = e.getMessage();
                return null;
            }            
        }
        else if(tipo.equals("profesor")) {
            Profesor pr = new Profesor();
            pr.setNombre(nombre);
            pr.setAPaterno(aPaterno);
            pr.setAMaterno(aMaterno);
            pr.setClaveUnica(claveUnica);
            pr.setPassword(password1);
            try {
                UserTransaction tx = (UserTransaction) new InitialContext().lookup("UserTransaction");
                tx.begin();
                    em.joinTransaction();
                    em.persist(pr);
                tx.commit();
                message = "Registro de profesor satisfactorio";
                return null; //cambiar
            }
            catch(Exception e) {
                message = e.getMessage();
                return null;
            }
        }
        else {
            message = "No coinciden los tipos de registro";
            return null;
        }
    }
}
